package com.prs.crm.action.person;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.person.Department;
import com.prs.crm.service.person.DepartmentService;

public class DepartmentAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Department department;
	private DepartmentService departmentService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private List<Department> dept;
	private Integer deptId;

	public List<Department> getDept() {
		return dept;
	}
	public void setDept(List<Department> dept) {
		this.dept = dept;
	}
	public Integer getDeptId() {
		return deptId;
	}
	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}	
	
	@Action("listDepartment")
	public String listDepartment(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Department.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDepartmentService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editDepartment")
	public String editDepartment(){
		this.dept=this.departmentService.getAllDepartment();
		return SUCCESS;
	}
	@Action("viewDepartment")
	public String viewDepartment(){
		return SUCCESS;
	}
	@Action("selectDept")
	public String selectDept(){
		return SUCCESS;
	}
	
//	@Action(value="ajaxDeptId",results = { @Result(name = "success", type = "json",
//			params = {"root", "dept" }) })
//	public String ajaxDeptId(){
//		dept=this.departmentService.getDepartmentByDeptId(deptId);
//		return SUCCESS;
//	}
//	
	@Action(value="saveDepartment",results={
			@Result(name="success",type="redirect",location="listDepartment"),
			@Result(name="error",type="chain",location="editDepartment")
	})
	public String saveDepartment(){
		this.departmentService.save(department);
		return SUCCESS;
	}
	@Action(value="removeDepartment",results={
			@Result(name="success",type="redirect",location="listDepartment"),
			@Result(name="error",type="chain",location="listDepartment")
	})
	public String removeDepartment(){
		if(ids!=null){
			this.department=this.getDepartmentService().get(ids[0]);
		}
		if(department.getParent()!=null){
			this.addActionError("数据有关联不能删除");
			return ERROR;
		}
		this.getDepartmentService().remove(department);
		return SUCCESS;
	}
}
